<!DOCTYPE html>
<!--
	需求：
    	1.点击Large Print按钮，body使用large类
        2.点击default按钮清除样式，
        	点击Narrow Column按钮body使用narrow类
        	点击Large Print按钮，body使用large类
        3.点击哪个按钮，这个按钮使用selected类，并清除其他按钮的样式
        4.使用jQuery的连缀能力，优化你的代码
        5.最终升级，简化你的代码
        6.点击按钮区域的标题，隐藏/显示按钮，使用hidden类
        7.鼠标悬停按钮标题使用hover类，移除时删除hover类
        8.对之前所有需求进行大规模的升级，通过点击整体按钮区域，实现功能
        9.对需求8的代码进行优化升级
        10.基于需求9，给后两个按钮添加功能，取消区域的折叠效果
        11.通过事件命名空间，让事件的移除更有针对性（暂停所有功能）
        12.折叠功能代码中id和命名空间过于凌乱繁琐，又一次小小的升级
        13.通过代码模拟用户的操作，默认点击一次#switcher(基于需求12)
        14.添加键盘操作功能，按D，N，L等同点击三个按钮
        15.组成完整的程序
-->
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>A Christmas Carol</title>

    <link rel="stylesheet" href="03.css" type="text/css" />
    <script src="js/jquery.js"></script>
    <script>
		$(document).ready(function(e) {
            //需求1
			//为按钮添加单击事件
			/*$("#switcher-large").on("click",function(){
				$("body").addClass("large");
			});*/

			//需求2
			/*$("#switcher-default").on("click",function(){
				$("body").removeClass("narrow");
				$("body").removeClass("large");
			});

			$("#switcher-narrow").on("click",function(){
				$("body").removeClass("large");
				$("body").addClass("narrow");
			});

			$("#switcher-large").on("click",function(){
				$("body").removeClass("narrow");
				$("body").addClass("large");
			});*/

			//需求3
			/*$("#switcher-default").on("click",function(){
				$("body").removeClass("narrow");
				$("body").removeClass("large");
				$("#switcher button").removeClass("selected");
				//this js对象
				$(this).addClass("selected");
			});

			$("#switcher-narrow").on("click",function(){
				$("body").removeClass("large");
				$("body").addClass("narrow");
				$("#switcher button").removeClass("selected");
				$(this).addClass("selected");
			});

			$("#switcher-large").on("click",function(){
				$("body").removeClass("narrow");
				$("body").addClass("large");
				$("#switcher button").removeClass("selected");
				$(this).addClass("selected");
			});*/

			//需求4
			//default按钮设置默认selected类
			/*$("#switcher-default").addClass("selected").on("click",function(){
				$("body").removeClass("narrow").removeClass("large");
			});

			$("#switcher-narrow").on("click",function(){
				$("body").removeClass("large").addClass("narrow");
			});

			$("#switcher-large").on("click",function(){
				$("body").removeClass("narrow").addClass("large");
			});

			$("#switcher button").on("click",function(){
				$("#switcher button").removeClass("selected");
				$(this).addClass("selected");
			});*/

			//需求5
			/*$("#switcher-default").addClass("selected");
			$("#switcher button").click(function() {
			    //点的按钮id  this.id
                var bodyClass = this.id.split("-")[1];
                $("body").removeClass().addClass(bodyClass);
                $("#switcher button").removeClass("selected");
                $(this).addClass("selected");
            });*/

			//需求6
			/*$("#switcher h3").click(function() {
                $("#switcher button").toggleClass("hidden");
            });

			//需求7
			//hover参数1，鼠标悬停，参数2，鼠标移除
			$("#switcher h3").hover(function(){
				$(this).addClass("hover");
			},function(){
				$(this).removeClass("hover");
			});*/

			//需求8
			/*$("#switcher").hover(function(){
				$(this).addClass("hover");
			},function(){
				$(this).removeClass("hover");
			});

			$("#switcher").click(function(e) {
				//事件源对象（jQuery对象）不是一个按钮对象
                if(!$(e.target).is("button")){
					$("#switcher button").toggleClass("hidden");
				}
            });

			$("#switcher-default").addClass("selected");

			$("#switcher").click(function(e) {
				//事件源对象是一个按钮
                if($(e.target).is("button")){
					//获取这个对象id后半部分，e.target取得的是js对象
					var bodyClass = e.target.id.split("-")[1];
					$("body").removeClass().addClass(bodyClass);
					$("#switcher button").removeClass("selected");
					$(e.target).addClass("selected");
				}
            });*/

			//需求9
			/*$("#switcher").hover(function(){
				$(this).addClass("hover");
			},function(){
				$(this).removeClass("hover");
			});

			$("#switcher-default").addClass("selected");

			$("#switcher").click(function(e) {
                if($(e.target).is("button")){
					var bodyClass = e.target.id.split("-")[1];
					$("body").removeClass().addClass(bodyClass);
					$("#switcher button").removeClass("selected");
					$(e.target).addClass("selected");
				}else{
					$("#switcher button").toggleClass("hidden");
				}
            });*/

			//需求10
			/*$("#switcher-large,#switcher-narrow").click(function(e) {
				//在#switcher单击功能被移除的同时，子元素的单击也被移除了
                $("#switcher").off("click");
            });*/

			//需求11
			//collapse是单击事件的命名空间(名自定义)
			/*$("#switcher").on("click.collapse",function(e){
				if(!$(e.target).is("button")){
					$("#switcher button").toggleClass("hidden");
				}
			});

			$("#switcher-large,#switcher-narrow").click(function(e) {
				//这里移除指定命名空间的单击事件
                $("#switcher").off("click.collapse");
            });

			$("#switcher-default").addClass("selected");

			$("#switcher button").click(function() {
                var bodyClass = this.id.split("-")[1];
				$("body").removeClass().addClass(bodyClass);
				$("#switcher button").removeClass("selected");
				$(this).addClass("selected");
            });*/


			//需求12
			/*let toggleSwitcher = function(e){
				if(!$(e.target).is("button")){
					$("#switcher button").toggleClass("hidden");
				}
			};

			$("#switcher").on("click",toggleSwitcher);

			$("#switcher button").click(function(e) {
                $("#switcher").off("click",toggleSwitcher);
            	if(this.id == "switcher-default"){
					$("#switcher").on("click",toggleSwitcher);
				}
            });
			$("#switcher-default").addClass("selected");

			$("#switcher button").click(function() {
                var bodyClass = this.id.split("-")[1];
				$("body").removeClass().addClass(bodyClass);
				$("#switcher button").removeClass("selected");
				$(this).addClass("selected");
            })
*/

			//需求13
			//$("#switcher").trigger("click");
			//$("#switcher").click();


			//需求14
			/*var triggers = {
				D:"default",
				N:"narrow",
				L:"large"
			};

			$(document).keyup(function(e) {
				//e.which按键对应的ASCII值,String.fromCharCode()获取对应的字母
                var key = String.fromCharCode(e.which);
				if(key in triggers){//判断triggers中是否包含这个属性，key属性名字符串
					$("#switcher-" + triggers[key]).click();//模拟一次点击
				}
            });*/


			//需求15 完整的功能
			//#switcher区域的悬停效果
			$("#switcher").hover(function(){
				$(this).addClass("hover");
			},function(){
				$(this).removeClass("hover");
			});

			//#switcher按钮区域的扩展和折叠
			var toggleSwitcher = function(e){
				if(!$(e.target).is("button")){
					$("#switcher button").toggleClass("hidden");
				}
			};
			//给#switcher的div注册一个点击事件
			$("#switcher").on("click",toggleSwitcher);
			//模拟一次点击,折叠上
			$("#switcher").click();
			//设置body文字样式，按钮样式，取消和添加#switcher区域折叠功能
			var setBodyClass = function(className){
				$("body").removeClass().addClass(className);
				$("#switcher button").removeClass("selected");
				$("#switcher-" + className).addClass("selected");
				$("#switcher").off("click",toggleSwitcher);
				if(className == "default"){
					$("#switcher").on("click",toggleSwitcher);
				}
			};
			//开始选中switcher-default按钮
			$("#switcher-default").addClass("selected");

			//当按钮被单击，调用setBodyClass()
			$("#switcher").click(function(e) {
                if($(e.target).is("button")){
					var bodyClass = e.target.id.split("-")[1];
					setBodyClass(bodyClass);
				}
            });
			//映射键码和对应的按钮
			var triggers = {
				D:"default",
				N:"narrow",
				L:"large"
			};
			//当按下对应的键时调用setBodyClass()
			$(document).keyup(function(e) {
				//e.keyCode == e.which;
                var key = String.fromCharCode(e.keyCode);
				if(key in triggers){
					setBodyClass(triggers[key]);
				}
            });
        });



		//#事件传播，e.stopPropagation
		//#阻止默认操作，e.preventDefault
	</script>

  </head>
  <body>
    <div id="container">

      <div id="switcher" class="switcher">
        <h3>Style Switcher</h3>
        <button id="switcher-default">
          Default
        </button>
        <button id="switcher-narrow">
          Narrow Column
        </button>
        <button id="switcher-large">
          Large Print
        </button>
      </div>

      <div id="header">
        <h2>A Christmas Carol</h2>
        <h2 class="subtitle">In Prose, Being a Ghost Story of Christmas</h2>
        <div class="author">by Charles Dickens</div>
      </div>

      <div class="chapter" id="chapter-preface">
        <h3 class="chapter-title">Preface</h3>
        <p>I HAVE endeavoured in this Ghostly little book, to raise the Ghost of an Idea, which shall not put my readers out of humour with themselves, with each other, with the season, or with me.  May it haunt their houses pleasantly, and no one wish to lay it.</p>
        <p>Their faithful Friend and Servant,</p>
        <p>C. D.</p>
        <p>December, 1843.</p>
      </div>

      <div class="chapter" id="chapter-1">
        <h3 class="chapter-title">Stave I: Marley's Ghost</h3>
        <p>MARLEY was dead: to begin with. There is no doubt whatever about that. The register of his burial was signed by the clergyman, the clerk, the undertaker, and the chief mourner. Scrooge signed it: and Scrooge's name was good upon 'Change, for anything he chose to put his hand to. Old Marley was as dead as a door-nail.</p>
        <p>Mind! I don't mean to say that I know, of my own knowledge, what there is particularly dead about a door-nail. I might have been inclined, myself, to regard a coffin-nail as the deadest piece of ironmongery in the trade. But the wisdom of our ancestors is in the simile; and my unhallowed hands shall not disturb it, or the Country's done for. You will therefore permit me to repeat, emphatically, that Marley was as dead as a door-nail.</p>
        <p>Scrooge knew he was dead? Of course he did. How could it be otherwise? Scrooge and he were partners for I don't know how many years. Scrooge was his sole executor, his sole administrator, his sole assign, his sole residuary legatee, his sole friend, and sole mourner. And even Scrooge was not so dreadfully cut up by the sad event, but that he was an excellent man of business on the very day of the funeral, and solemnised it with an undoubted bargain.</p>
        <p>The mention of Marley's funeral brings me back to the point I started from. There is no doubt that Marley was dead. This must be distinctly understood, or nothing wonderful can come of the story I am going to relate. If we were not perfectly convinced that Hamlet's Father died before the play began, there would be nothing more remarkable in his taking a stroll at night, in an easterly wind, upon his own ramparts, than there would be in any other middle-aged gentleman rashly turning out after dark in a breezy spot&mdash;say Saint Paul's Churchyard for instance&mdash; literally to astonish his son's weak mind.</p>
        <p>Scrooge never painted out Old Marley's name. There it stood, years afterwards, above the warehouse door: Scrooge and Marley. The firm was known as Scrooge and Marley. Sometimes people new to the business called Scrooge Scrooge, and sometimes Marley, but he answered to both names. It was all the same to him.</p>
        <p>Oh! But he was a tight-fisted hand at the grind-stone, Scrooge! a squeezing, wrenching, grasping, scraping, clutching, covetous, old sinner! Hard and sharp as flint, from which no steel had ever struck out generous fire; secret, and self-contained, and solitary as an oyster. The cold within him froze his old features, nipped his pointed nose, shrivelled his cheek, stiffened his gait; made his eyes red, his thin lips blue; and spoke out shrewdly in his grating voice. A frosty rime was on his head, and on his eyebrows, and his wiry chin. He carried his own low temperature always about with him; he iced his office in the dog-days; and didn't thaw it one degree at Christmas.</p>
        <p>External heat and cold had little influence on Scrooge. No warmth could warm, no wintry weather chill him. No wind that blew was bitterer than he, no falling snow was more intent upon its purpose, no pelting rain less open to entreaty. Foul weather didn't know where to have him. The heaviest rain, and snow, and hail, and sleet, could boast of the advantage over him in only one respect. They often "came down" handsomely, and Scrooge never did.</p>
        <p>Nobody ever stopped him in the street to say, with gladsome looks, "My dear Scrooge, how are you? When will you come to see me?" No beggars implored him to bestow a trifle, no children asked him what it was o'clock, no man or woman ever once in all his life inquired the way to such and such a place, of Scrooge. Even the blind men's dogs appeared to know him; and when they saw him coming on, would tug their owners into doorways and up courts; and then would wag their tails as though they said, "No eye at all is better than an evil eye, dark master!"</p>
        <p>But what did Scrooge care! It was the very thing he liked. To edge his way along the crowded paths of life, warning all human sympathy to keep its distance, was what the knowing ones call "nuts" to Scrooge.</p>
        </div>
    </div>
  </body>
</html>
